#include "FileUtils/String.h"
namespace gengine
{
namespace utils
{

String::String(const char *str)
{
	assign(str);
}

String::String(std::string str)
{
	assign(str);
}

String::String(char c)
{
	 clear();
   push_back(c);
}

int String::toInt()
{
	return atoi(c_str());
}

double String::toDouble()
{
	return atof(c_str());
}

bool String::toBool()
{   
	String str(*this);
	
	if(str == "true") return true;
	else return false;
}

Vector2D String::toVector2D()
{
	Vector2D vec(0.0,0.0);
	String str(*this);
	int i = 0;

	while(str.rfind(",")!=str.npos|| i!=2)
	{
		vec[i] = String(str.substr(0,str.find(","))).toDouble();
		str.erase(0,str.find(",")+1);
		i++;

		if(str.find(",")==str.npos)
		{
			vec[i] = String(str.substr(0,npos)).toDouble();
			break;
		}
	}

   return vec;

}

Vector3D String::toVector3D()
{
	Vector3D vec(0.0,0.0,0.0);
	String str(*this);
	int i = 0;

	while(str.rfind(",")!=str.npos|| i!=3)
	{
		vec[i] = String(str.substr(0,str.find(","))).toDouble();
		str.erase(0,str.find(",")+1);
		i++;

		if(str.find(",")==str.npos)
		{
			vec[i] = String(str.substr(0,npos)).toDouble();
			break;
		}
	}

   return vec;

}

Vector4D String::toVector4D()
{
	Vector4D vec(0.0,0.0,0.0,0.0);
	String str(*this);
	int i = 0;

	while(str.rfind(",")!=str.npos|| i!=4)
	{
		vec[i] = String(str.substr(0,str.find(","))).toDouble();
		str.erase(0,str.find(",")+1);
		i++;

		if(str.find(",")==str.npos)
		{
			vec[i] = String(str.substr(0,npos)).toDouble();
			break;
		}
	}

   return vec;

}

const String & operator +=(String& left,int a)
{
	 char buf[10];
		
		 
	 itoa(a,buf,10);

		
	 left+=buf;

		 
	 return left;
}

const String & String:: operator +(int a){
		
		 char buf[10];
		
		 String str(*this);
		 
		 itoa(a,buf,10);

		
		str = str+buf;
		 
		return str;
	 }

String	String::getFileExtension() const
{
	size_t n = (*this).find_last_of( '.' );
	if ( n != (*this).npos )
	{
		return (*this).substr( n );
	}
	else
		return "";
}

String	String::getFileName() const
{
	size_t n = (*this).find_last_of( '/' );
	if ( n == (*this).npos )
		n = (*this).find_last_of( '\\' );

	if ( n != (*this).npos )
	{
		String fn = (*this).substr( n + 1, (*this).length() - n - 1 ); 
		n = (*this).find_last_of( '.' );
		if ( n != (*this).npos )
			fn.erase(n+1);
		return fn;
	}
	else
		return "";
}
}
}



